{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 9.10 \u4e3a\u7c7b\u548c\u9759\u6001\u65b9\u6cd5\u63d0\u4f9b\u88c5\u9970\u5668\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u7ed9\u7c7b\u6216\u9759\u6001\u65b9\u6cd5\u63d0\u4f9b\u88c5\u9970\u5668\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u7ed9\u7c7b\u6216\u9759\u6001\u65b9\u6cd5\u63d0\u4f9b\u88c5\u9970\u5668\u662f\u5f88\u7b80\u5355\u7684\uff0c\u4e0d\u8fc7\u8981\u786e\u4fdd\u88c5\u9970\u5668\u5728 @classmethod \u6216 @staticmethod \u4e4b\u524d\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import time\nfrom functools import wraps\n\n# A simple decorator\ndef timethis(func):\n    @wraps(func)\n    def wrapper(*args, **kwargs):\n        start = time.time()\n        r = func(*args, **kwargs)\n        end = time.time()\n        print(end-start)\n        return r\n    return wrapper\n\n# Class illustrating application of the decorator to different kinds of methods\nclass Spam:\n    @timethis\n    def instance_method(self, n):\n        print(self, n)\n        while n > 0:\n            n -= 1\n\n    @classmethod\n    @timethis\n    def class_method(cls, n):\n        print(cls, n)\n        while n > 0:\n            n -= 1\n\n    @staticmethod\n    @timethis\n    def static_method(n):\n        print(n)\n        while n > 0:\n            n -= 1"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u88c5\u9970\u540e\u7684\u7c7b\u548c\u9759\u6001\u65b9\u6cd5\u53ef\u6b63\u5e38\u5de5\u4f5c\uff0c\u53ea\u4e0d\u8fc7\u589e\u52a0\u4e86\u989d\u5916\u7684\u8ba1\u65f6\u529f\u80fd\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "s = Spam()\ns.instance_method(1000000)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "Spam.class_method(1000000)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "Spam.static_method(1000000)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u628a\u88c5\u9970\u5668\u7684\u987a\u5e8f\u5199\u9519\u4e86\u5c31\u4f1a\u51fa\u9519\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u4f60\u50cf\u4e0b\u9762\u8fd9\u6837\u5199\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "class Spam:\n    @timethis\n    @staticmethod\n    def static_method(n):\n        print(n)\n        while n > 0:\n            n -= 1"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u90a3\u4e48\u4f60\u8c03\u7528\u8fd9\u4e2a\u9759\u6001\u65b9\u6cd5\u65f6\u5c31\u4f1a\u62a5\u9519\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "Spam.static_method(1000000)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u95ee\u9898\u5728\u4e8e @classmethod \u548c @staticmethod \u5b9e\u9645\u4e0a\u5e76\u4e0d\u4f1a\u521b\u5efa\u53ef\u76f4\u63a5\u8c03\u7528\u7684\u5bf9\u8c61\uff0c\n\u800c\u662f\u521b\u5efa\u7279\u6b8a\u7684\u63cf\u8ff0\u5668\u5bf9\u8c61(\u53c2\u80038.9\u5c0f\u8282)\u3002\u56e0\u6b64\u5f53\u4f60\u8bd5\u7740\u5728\u5176\u4ed6\u88c5\u9970\u5668\u4e2d\u5c06\u5b83\u4eec\u5f53\u505a\u51fd\u6570\u6765\u4f7f\u7528\u65f6\u5c31\u4f1a\u51fa\u9519\u3002\n\u786e\u4fdd\u8fd9\u79cd\u88c5\u9970\u5668\u51fa\u73b0\u5728\u88c5\u9970\u5668\u94fe\u4e2d\u7684\u7b2c\u4e00\u4e2a\u4f4d\u7f6e\u53ef\u4ee5\u4fee\u590d\u8fd9\u4e2a\u95ee\u9898\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5f53\u6211\u4eec\u5728\u62bd\u8c61\u57fa\u7c7b\u4e2d\u5b9a\u4e49\u7c7b\u65b9\u6cd5\u548c\u9759\u6001\u65b9\u6cd5(\u53c2\u80038.12\u5c0f\u8282)\u65f6\uff0c\u8fd9\u91cc\u8bb2\u5230\u7684\u77e5\u8bc6\u5c31\u5f88\u6709\u7528\u4e86\u3002\n\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u60f3\u5b9a\u4e49\u4e00\u4e2a\u62bd\u8c61\u7c7b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u4f7f\u7528\u7c7b\u4f3c\u4e0b\u9762\u7684\u4ee3\u7801\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from abc import ABCMeta, abstractmethod\nclass A(metaclass=ABCMeta):\n    @classmethod\n    @abstractmethod\n    def method(cls):\n        pass"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c@classmethod \u8ddf @abstractmethod \u4e24\u8005\u7684\u987a\u5e8f\u662f\u6709\u8bb2\u7a76\u7684\uff0c\u5982\u679c\u4f60\u8c03\u6362\u5b83\u4eec\u7684\u987a\u5e8f\u5c31\u4f1a\u51fa\u9519\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}